//
//  SinaHomeViewModel.m
//  MySinaWeiboProjectForOC
//
//  Created by apple on 2016/11/17.
//  Copyright © 2016年 WangChen. All rights reserved.
//

#import "SinaHomeViewModel.h"
#import "SinaUserAccountViewModel.h"
#import "MyAFNTools.h"
#import <YYModel.h>
#import "SinaHomeCellViewModel.h"
#import "SinaHomeCellModel.h"
#import <SDWebImageManager.h>

@implementation SinaHomeViewModel

#pragma mark
#pragma mark - 加载数据
-(void)loadWeiboDataWithIsPullUp:(BOOL)isPullUp finishBlock:(void (^)(BOOL, NSInteger))finish{
    NSDictionary *paramter = @{@"access_token":[SinaUserAccountViewModel shared].userAccountModel.access_token,@"since_id":@0};
    
    if(isPullUp){
        NSInteger maxID = self.homeCellViewModelList.lastObject.homeCellModel.messageID - 1;
        
        paramter = @{@"access_token" : [SinaUserAccountViewModel shared].userAccountModel.access_token,
                     @"max_id" : @(maxID)};
    }
    
    NSInteger sinceID;
    if(self.homeCellViewModelList.count != 0 && !isPullUp){
        sinceID = self.homeCellViewModelList.firstObject.homeCellModel.messageID;
        paramter = @{@"access_token":[SinaUserAccountViewModel shared].userAccountModel.access_token,@"since_id":@(sinceID)};
    }
    
    [[MyAFNTools sharedOwnTool]requestWithURLStr:weiboURLString method:GET paramter:paramter complete:^(NSDictionary* responseObj, NSError *error) {
        if(error){
            finish(NO,0);
            return;
        }
        
        NSArray *tempDictArray = responseObj[@"statuses"];
        NSMutableArray <SinaHomeCellViewModel*>*homeCellViewModelList = [NSMutableArray array];
        for (NSDictionary *dict in tempDictArray) {
            SinaHomeCellModel *cellModel = [[SinaHomeCellModel alloc]init];
            [cellModel yy_modelSetWithJSON:dict];
            SinaHomeCellViewModel *cellViewModel = [[SinaHomeCellViewModel alloc]init];
            cellViewModel.homeCellModel = cellModel;
            [homeCellViewModelList addObject:cellViewModel];
        }
        
        //记录刷新了多少条
        NSInteger refreshCount = tempDictArray.count;
        
        //如果是上拉加载更多
        if(isPullUp){
            [self.homeCellViewModelList addObjectsFromArray:homeCellViewModelList];
        }else{
            self.homeCellViewModelList = [homeCellViewModelList arrayByAddingObjectsFromArray:self.homeCellViewModelList].mutableCopy;
        }
        
        dispatch_group_t group = dispatch_group_create();
        
        for (SinaHomeCellViewModel *cellViewModel in homeCellViewModelList) {
            if(cellViewModel.pictureUrlInfoModelArray.count == 1){
                dispatch_group_enter(group);
                NSURL *imageURL = [NSURL URLWithString:cellViewModel.pictureUrlInfoModelArray.firstObject.thumbnail_pic];
                
                [[SDWebImageManager sharedManager] downloadImageWithURL:imageURL options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                    dispatch_group_leave(group);
                }];
            }
        }
        
        dispatch_group_notify(group, dispatch_get_main_queue(), ^{
            finish(YES,refreshCount);
        });
        
        return;
    }];
}




-(NSMutableArray<SinaHomeCellViewModel *> *)homeCellViewModelList{
    if(_homeCellViewModelList == nil){
        _homeCellViewModelList = [NSMutableArray array];
    }
    return _homeCellViewModelList;
}


@end
